﻿
//时间拓展

String.formatDate = function (format) {
    return formatDate(this, format);
}

function formatDate(date, format) {
    if (!date || !format)
        return "";

    if (date.constructor == String) {
        date = date.replace(/-/g, "/").replace("T", " ");//IE11里面不能直接转换带"-",必须先替换成"/"，如果有T也要替换，不然 2018/1/1T10:10 这种格式会有问题
        date = new Date(date);
    }
    if (date.constructor == Date) {
        return date.format(format);
    }
    return date;
}

Date.prototype.format = function (format) {
    var o =
        {
            "M+": this.getMonth() + 1, //month
            "d+": this.getDate(),    //day
            "H+": this.getHours(),   //hour
            "m+": this.getMinutes(), //minute
            "s+": this.getSeconds(), //second
            "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
            "S": this.getMilliseconds() //millisecond
        };
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};